from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.response import Response


class MultipleDestroyMixin:
    """
    定义批量删除的试图函数
    """
    del_ids = openapi.Schema(type=openapi.TYPE_OBJECT, required=['ids'], properties={
        'ids': openapi.Schema(type=openapi.TYPE_ARRAY, items=openapi.Schema(type=openapi.TYPE_INTEGER),
                              description="选择要删除那些需要删除的ID（主键）列表")
    })

    @swagger_auto_schema(methods=['delete'], request_body=del_ids, operation_description="批量删除")
    @action(methods=['DELETE'], detail=False)
    def multiple_delete(self, request, *args, **kwargs):
        # 定义批量删除的函数
        delete_ids = request.data.get('ids')
        if not delete_ids:
            return Response(data={"detail": "参数错误，ids为必传参数"}, status=status.HTTP_400_BAD_REQUEST)
        if not isinstance(delete_ids, list):
            return Response(data={"detail": "参数类型错误，必须是列表类型"}, status=status.HTTP_400_BAD_REQUEST)
        # 子类一会儿继承的 get_queryset
        queryset = self.get_queryset()
        del_queryset = queryset.filter(id__in=delete_ids)
        if del_queryset.count != len(delete_ids):
            return Response(data={'detail': "删除的数据不存在"}, status=status.HTTP_400_BAD_REQUEST)
        del_queryset.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)
